package main

func main() {

	//strsArray := []string{"a", "b", "c"}
	//str := "sssssss"

	//测试用例1
	l3 := ListNode{
		Val:  4,
		Next: nil,
	}
	l2 := ListNode{
		Val:  2,
		Next: &l3,
	}
	l1 := ListNode{
		Val:  1,
		Next: &l2,
	}
	//测试用例2：
	ll3 := ListNode{
		Val:  4,
		Next: nil,
	}
	ll2 := ListNode{
		Val:  3,
		Next: &ll3,
	}
	ll1 := ListNode{
		Val:  1,
		Next: &ll2,
	}

	//打印测试用例返回结果
	//println(l1.Val, l1.Next.Val, l1.Next.Next.Val)
	//println(ll1.Val, ll1.Next.Val, ll1.Next.Next.Val)
	//println(resp)
	resp := mergeTwoLists(&l1, &ll1)
	for resp != nil {
		println(resp.Val)
		resp = resp.Next
	}

}

//Definition for singly-linked list.
type ListNode struct {
	Val  int
	Next *ListNode
}

func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
	//newList := ListNode{}
	//让startnode指向一个新地址空间，然后让newlist指向startnode，所以现在newlist也是新的地址空间了
	startNode := new(ListNode)
	var newList *ListNode
	newList = startNode

	for l1 != nil && l2 != nil { //l1和l2都不为nil才可以走
		if l1.Val <= l2.Val {
			newList.Next = l1
			(*newList).Next = l1
			l1 = l1.Next
		} else {
			newList.Next = l2
			l2 = l2.Next
		}
		newList = newList.Next //newList步进
	}
	if l1 != nil {
		newList.Next = l1
	}
	if l2 != nil {
		newList.Next = l2
	}

	return startNode.Next
}
